package com.guaoran.micro.services.spring.boot.starter.autoconfigurate;

import com.alibaba.fastjson.JSONObject;
import com.guaoran.micro.services.spring.boot.starter.format.DefaultFormatProcessor;
import com.guaoran.micro.services.spring.boot.starter.format.JsonFormatProcessor;
import com.guaoran.micro.services.spring.boot.starter.service.FormatProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @Author gucheng
 * @Description 配置类
 * 2019-07-10 14:04
 */
@Configuration // 由于在 spring.factories 中已经配置了该类，该注解可以不加
public class FormatConfiguration {
    /**
     * 默认处理类 如果没有 {@link JSONObject} 则装配 {@link DefaultFormatProcessor} 处理
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSONObject")
    public FormatProcessor defaultFormatProcessor(){
        return new DefaultFormatProcessor();
    }

    /**
     * json 格式 如果存在 {@link JSONObject} 则装配 {@link JsonFormatProcessor} 处理
     * @return
     */
    @Bean
    @ConditionalOnClass(name = "com.alibaba.fastjson.JSONObject")
    public FormatProcessor jsonFormatProcessor(){
        return new JsonFormatProcessor();
    }

}
